<template>
  <div class="register-container">
    <!-- 背景装饰 -->
    <div class="bg-decoration">
      <div class="floating-shape shape-1"></div>
      <div class="floating-shape shape-2"></div>
      <div class="floating-shape shape-3"></div>
      <div class="floating-shape shape-4"></div>
    </div>
    
    <el-card class="register-card" :class="{ 'card-entrance': cardVisible }">
      <template #header>
        <div class="card-header">
          <el-icon class="header-icon"><UserFilled /></el-icon>
          <span class="title">创建新账户</span>
          <div class="subtitle">加入我们，开启美食之旅</div>
        </div>
      </template>
      
      <el-form ref="formRef" :model="registerForm" :rules="rules" label-width="0" class="register-form">
        <div class="form-row">
          <el-form-item prop="username" class="form-item-half">
            <el-input 
              v-model="registerForm.username" 
              placeholder="用户名" 
              clearable
              size="large"
              class="custom-input"
            >
              <template #prefix>
                <el-icon class="input-icon"><User /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          
          <el-form-item prop="nickname" class="form-item-half">
            <el-input 
              v-model="registerForm.nickname" 
              placeholder="昵称" 
              clearable
              size="large"
              class="custom-input"
            >
              <template #prefix>
                <el-icon class="input-icon"><Avatar /></el-icon>
              </template>
            </el-input>
          </el-form-item>
        </div>
        
        <div class="form-row">
          <el-form-item prop="password" class="form-item-half">
            <el-input
              v-model="registerForm.password"
              type="password"
              placeholder="密码"
              show-password
              clearable
              size="large"
              class="custom-input"
            >
              <template #prefix>
                <el-icon class="input-icon"><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          
          <el-form-item prop="confirmPassword" class="form-item-half">
            <el-input
              v-model="registerForm.confirmPassword"
              type="password"
              placeholder="确认密码"
              show-password
              clearable
              size="large"
              class="custom-input"
            >
              <template #prefix>
                <el-icon class="input-icon"><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
        </div>
        
        <el-form-item prop="sex" class="gender-section">
          <div class="gender-row">
            <span class="gender-label">性别</span>
            <el-radio-group v-model="registerForm.sex" class="gender-group">
              <el-radio label="0" class="gender-radio">男</el-radio>
              <el-radio label="1" class="gender-radio">女</el-radio>
              <el-radio label="2" class="gender-radio">未知</el-radio>
            </el-radio-group>
          </div>
        </el-form-item>
        
        <div class="form-row">
          <el-form-item prop="phonenumber" class="form-item-half">
            <el-input 
              v-model="registerForm.phonenumber" 
              placeholder="手机号" 
              clearable
              size="large"
              class="custom-input"
            >
              <template #prefix>
                <el-icon class="input-icon"><Phone /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          
          <el-form-item prop="email" class="form-item-half">
            <el-input 
              v-model="registerForm.email" 
              placeholder="邮箱" 
              clearable
              size="large"
              class="custom-input"
            >
              <template #prefix>
                <el-icon class="input-icon"><Message /></el-icon>
              </template>
            </el-input>
          </el-form-item>
        </div>
        
        <!-- 用户角色选择 -->
        <el-form-item prop="userType" class="role-section">
          <div class="role-row">
            <span class="role-label">注册类型</span>
            <el-radio-group v-model="registerForm.userType" class="role-group">
              <el-radio label="1" class="role-radio">普通用户</el-radio>
              <el-radio label="0" class="role-radio">管理员</el-radio>
            </el-radio-group>
          </div>
        </el-form-item>
        
        <el-form-item>
          <el-button 
            type="primary" 
            :loading="loading" 
            @click="handleRegister" 
            class="register-btn"
            size="large"
          >
            <span v-if="!loading">立即注册</span>
            <span v-else>注册中...</span>
          </el-button>
        </el-form-item>
        
        <el-form-item>
          <div class="footer-links">
            <span>已有账号？</span>
            <el-link type="primary" @click="goToLogin" class="login-link">
              立即登录
            </el-link>
          </div>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { register } from '@/api/user';
import { UserFilled, User, Avatar, Lock, Phone, Message } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';
import type { FormInstance, FormRules } from 'element-plus';

const router = useRouter();
const formRef = ref<FormInstance>();
const loading = ref(false);
const cardVisible = ref(false);

const registerForm = reactive({
  username: '',
  nickname: '',
  password: '',
  confirmPassword: '',
  sex: '2' as '0' | '1' | '2',
  phonenumber: '',
  email: '',
  userType: '1' as '0' | '1' // 默认为普通用户
});

const validatePassword = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请再次输入密码'));
  } else if (value !== registerForm.password) {
    callback(new Error('两次输入密码不一致'));
  } else {
    callback();
  }
};

const validatePhone = (rule: any, value: any, callback: any) => {
  if (value && !/^1[3-9]\d{9}$/.test(value)) {
    callback(new Error('请输入正确的手机号'));
  } else {
    callback();
  }
};

const validateEmail = (rule: any, value: any, callback: any) => {
  if (value && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value)) {
    callback(new Error('请输入正确的邮箱地址'));
  } else {
    callback();
  }
};

const rules: FormRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 30, message: '用户名长度在 3 到 30 个字符', trigger: 'blur' }
  ],
  nickname: [
    { required: true, message: '请输入昵称', trigger: 'blur' },
    { min: 2, max: 30, message: '昵称长度在 2 到 30 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少为 6 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, validator: validatePassword, trigger: 'blur' }
  ],
  phonenumber: [
    { validator: validatePhone, trigger: 'blur' }
  ],
  email: [
    { validator: validateEmail, trigger: 'blur' }
  ]
};

const handleRegister = async () => {
  if (!formRef.value) return;
  
  await formRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true;
      try {
        const response = await register(registerForm);
        if (response.operateResult === 'SUCC') {
          ElMessage.success('注册成功，请登录');
          router.push('/login');
        } else {
          ElMessage.error(response.errorMsg || '注册失败');
        }
      } catch (error: any) {
        ElMessage.error(error.message || '注册失败');
      } finally {
        loading.value = false;
      }
    }
  });
};

const goToLogin = () => {
  router.push('/login');
};

// 组件挂载时触发动画
onMounted(() => {
  setTimeout(() => {
    cardVisible.value = true;
  }, 100);
});
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 100vw;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: fixed;
  top: 0;
  left: 0;
  overflow: hidden;
}

/* 背景装饰 */
.bg-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.floating-shape {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 6s ease-in-out infinite;
}

.shape-1 {
  width: 60px;
  height: 60px;
  top: 15%;
  left: 8%;
  animation-delay: 0s;
}

.shape-2 {
  width: 100px;
  height: 100px;
  top: 40%;
  right: 12%;
  animation-delay: 1.5s;
}

.shape-3 {
  width: 80px;
  height: 80px;
  bottom: 25%;
  left: 15%;
  animation-delay: 3s;
}

.shape-4 {
  width: 70px;
  height: 70px;
  top: 70%;
  right: 25%;
  animation-delay: 4.5s;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
  }
}

/* 注册卡片 */
.register-card {
  width: 460px;
  max-width: 90vw;
  border-radius: 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(10px);
  background: rgba(255, 255, 255, 0.95);
  border: 1px solid rgba(255, 255, 255, 0.2);
  opacity: 0;
  transform: translateY(50px) scale(0.9);
  transition: all 0.8s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.card-entrance {
  opacity: 1;
  transform: translateY(0) scale(1);
}

.card-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px 0;
}

.header-icon {
  font-size: 48px;
  margin-bottom: 15px;
  color: #67c23a;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
}

.title {
  font-size: 28px;
  font-weight: 700;
  color: #303133;
  margin-bottom: 8px;
  background: linear-gradient(135deg, #67c23a, #409eff);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.subtitle {
  font-size: 14px;
  color: #909399;
  text-align: center;
}

/* 表单样式 */
.register-form {
  padding: 10px 25px 20px;
}

.form-row {
  display: flex;
  gap: 12px;
  margin-bottom: 12px;
}

.form-item-half {
  flex: 1;
  margin-bottom: 0;
}

.custom-input {
  margin-bottom: 0;
}

.custom-input :deep(.el-input__wrapper) {
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  border: 2px solid transparent;
  transition: all 0.3s ease;
  padding: 12px 16px;
}

.custom-input :deep(.el-input__wrapper:hover) {
  border-color: #67c23a;
  box-shadow: 0 6px 20px rgba(103, 194, 58, 0.2);
}

.custom-input :deep(.el-input__wrapper.is-focus) {
  border-color: #67c23a;
  box-shadow: 0 0 0 3px rgba(103, 194, 58, 0.1);
}

.input-icon {
  color: #909399;
  font-size: 18px;
}

/* 性别选择 */
.gender-section {
  margin: 12px 0;
}

.gender-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
}

.gender-label {
  font-size: 14px;
  color: #606266;
  font-weight: 500;
  margin-right: 20px;
  flex-shrink: 0;
}

.gender-group {
  display: flex;
  gap: 25px;
  flex: 1;
}

.gender-radio {
  margin-right: 0;
}

.gender-radio :deep(.el-radio__label) {
  font-size: 16px;
  color: #606266;
}

.gender-radio :deep(.el-radio__input.is-checked .el-radio__inner) {
  background-color: #67c23a;
  border-color: #67c23a;
}

/* 角色选择 */
.role-section {
  margin: 12px 0;
}

.role-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
}

.role-label {
  font-size: 14px;
  color: #606266;
  font-weight: 500;
  margin-right: 20px;
  flex-shrink: 0;
}

.role-group {
  display: flex;
  gap: 25px;
  flex: 1;
}

.role-radio {
  margin-right: 0;
}

.role-radio :deep(.el-radio__label) {
  font-size: 16px;
  color: #606266;
}

.role-radio :deep(.el-radio__input.is-checked .el-radio__inner) {
  background-color: #67c23a;
  border-color: #67c23a;
}

/* 注册按钮 */
.register-btn {
  width: 100%;
  height: 42px;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 600;
  background: linear-gradient(135deg, #67c23a, #409eff);
  border: none;
  box-shadow: 0 8px 20px rgba(103, 194, 58, 0.3);
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
  margin-top: 5px;
}

.register-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 30px rgba(103, 194, 58, 0.4);
}

.register-btn:active {
  transform: translateY(0);
}

.register-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s;
}

.register-btn:hover::before {
  left: 100%;
}

/* 底部链接 */
.footer-links {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  margin-top: 20px;
}

.login-link {
  font-weight: 600;
  text-decoration: none;
  transition: all 0.3s ease;
}

.login-link:hover {
  transform: scale(1.05);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .register-card {
    width: 95%;
    margin: 10px;
  }
  
  .register-form {
    padding: 20px 30px 30px;
  }
  
  .form-row {
    flex-direction: column;
    gap: 0;
  }
  
  .form-item-half {
    margin-bottom: 20px;
  }
  
  .title {
    font-size: 24px;
  }
  
  .header-icon {
    font-size: 40px;
  }
  
  .gender-group {
    flex-direction: column;
    gap: 15px;
    align-items: center;
  }
}

/* 加载动画 */
.register-btn.is-loading {
  pointer-events: none;
}

.register-btn.is-loading::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 20px;
  height: 20px;
  margin: -10px 0 0 -10px;
  border: 2px solid transparent;
  border-top: 2px solid #ffffff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
</style>

